// 工具函数

/**
 * 格式化日期为 YYYY-MM-DD 格式
 */
export const formatDate = (date: Date): string => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  return [year, month, day].map(formatNumber).join('-')
}

/**
 * 格式化时间为 HH:mm:ss 格式
 */
export const formatTime = (date: Date): string => {
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [hour, minute, second].map(formatNumber).join(':')
}

/**
 * 格式化数字，小于10前面补0
 */
const formatNumber = (n: number): string => {
  return n < 10 ? `0${n}` : `${n}`
}

/**
 * 生成唯一ID
 */
export const generateUniqueId = (): string => {
  return 'id_' + Date.now() + '_' + Math.floor(Math.random() * 1000)
}

/**
 * 获取当前日期的星期几
 */
export const getWeekday = (date: Date): string => {
  const weekdays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
  return weekdays[date.getDay()]
}

/**
 * 格式化完整日期时间，形如：2023年05月21日 周日 15:30
 */
export const formatFullDateTime = (date: Date): string => {
  const year = date.getFullYear()
  const month = formatNumber(date.getMonth() + 1)
  const day = formatNumber(date.getDate())
  const weekday = getWeekday(date)
  const hour = formatNumber(date.getHours())
  const minute = formatNumber(date.getMinutes())
  
  return `${year}年${month}月${day}日 ${weekday} ${hour}:${minute}`
}

/**
 * 显示提示信息
 */
export const showToast = (title: string, icon: 'success' | 'error' | 'loading' | 'none' = 'none'): void => {
  wx.showToast({
    title,
    icon,
    duration: 2000
  })
}
